#include "Input.h"
#include "WinGlobal.h"

Input::Input()
{
	//direct input
	DirectInput8Create(g_hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDIObject, NULL); 
	m_pDIObject->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboard, NULL);
	m_pDIObject->CreateDevice(GUID_SysMouse, &m_pDIMouse, NULL);

	m_pDIKeyboard->SetCooperativeLevel(g_wndHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	m_pDIMouse->SetCooperativeLevel(g_wndHandle, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 

	m_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
	m_pDIMouse->SetDataFormat(&c_dfDIMouse2);
}

bool Input::Update()
{
	m_pDIKeyboard->Acquire();
    m_pDIMouse->Acquire();
	
    m_pDIKeyboard->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);

	m_pDIMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &mouseState);

	if(buffer[DIK_ESCAPE] & 0x80)
		return false;
	return true;
}

InputStruct Input::getInput()
{
	InputStruct input;

	POINT pos;
	GetCursorPos(&pos);
	ScreenToClient(g_wndHandle, &pos);
	input.mousePos = XY((float)pos.x, (float)pos.y);

	input.mouseClicked = (mouseState.rgbButtons[0] & 0x80);

	for(int i=0;i<KEYBOARD_BUFF_SIZE;++i)
		input.keyBoard[i] = (buffer[i] & 0x80);

	return input;
}